package com.wd.base

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import java.lang.reflect.ParameterizedType

/**
 * 作者: 黄宋祥
 * 时间: 2024/9/9  9:24
 * 描述:
 */
abstract class BaseActivity<V:ViewModel,D:ViewDataBinding>(private var layoutId:Int) :AppCompatActivity(){
    lateinit var viewModel: V
    lateinit var dataBinding: D

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        dataBinding=DataBindingUtil.setContentView(this,layoutId)
        dataBinding.lifecycleOwner=this
        val parameterizedType = javaClass.genericSuperclass as ParameterizedType
        val clazz = parameterizedType.actualTypeArguments[0] as Class<V>
        viewModel=ViewModelProvider(this).get(clazz)
        initView()
        dataObserver()
    }

    abstract fun dataObserver()
    abstract fun initView()

    override fun onDestroy() {
        super.onDestroy()
        if (dataBinding!=null){
            dataBinding.unbind()
        }
    }
}